library(foreign)
library(readstata13)
library(Amelia)
library(dplyr)

health<-read.dta13("/health.dta")

use<-health[,c("country_id", "year", "e_peinfmor_ln", "v2x_EDcomp_thick", 
              "nocorrupt", "e_migdppc_ln", "e_miurbani", "e_migdpgro",
              "e_peinfmor_ln_rt")]

use<-use[!is.na(use$country_id),] 
use<-use[!is.na(use$year),] 
use<-use[use$year<=2010,] 
use<-use[rowSums(is.na(use[,c(3:9)]))<7,]
use$id<-seq_len(nrow(use))

mi.out <- amelia(use, m = 5, cs = "country_id",ts = "year",lags = "e_peinfmor_ln",
                 idvars = c("id"))  

write.amelia(mi.out, separate = FALSE, file.stem = "mi_data", format = "dta")
